<?php
    define('CONFIG_FILE',true);
    include '../config.php';
    
    define('DB_FILE',true);
    require_once '../Tool/DB/SqlDBManager.class.php';
    require_once '../Tool/Common/PrepareInput.php';

    session_start();
    if (empty($_SESSION["userType"])) {
        $url="../Index.php";
		include_once '../Tool/Common/Redirect.php';
		exit();
    }
    
    if(!empty($_SESSION['sortBy'])){
        $sort=$_SESSION['sortBy'];
    }else{
        $sort="client.NRIC";
    }
    $order='asc';

    $startDate=$endDate=$centreId="";  
    
    if(isset($_SESSION['startDate'])){ 
        $startDate=prepareInput($_SESSION['startDate']); 
        $startDate = date('Y-m-d', strtotime($_SESSION['startDate']));
    }
    if(isset($_SESSION['endDate'])) { 
        $endDate=prepareInput($_SESSION['endDate']);
        $endDate = date('Y-m-d', strtotime($_SESSION['endDate'])); 
    }
    if(isset($_SESSION['centreId'])){ 
        $centreId=prepareInput($_SESSION['centreId']); 
    }

    //Validate input
    if(!preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/",$startDate)){
        echo "<font color=red>Invalid Start Date Format.</font><br/>";
        exit();
    }
    if(!preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/",$endDate)){
        echo "<font color=red>Invalid End Date Format.</font><br/>";
        exit();
    }
    if(!preg_match("/^[0-9]*$/",$centreId)){
        echo "<font color=red>Invalid Centre ID.</font><br/>";
        exit();
    }

    for ($i = 0; $i < 100; $i+=5) {
        ${'beforeDeviationSC' . $i} = 0;
    }
    for ($i = 0; $i < 100; $i+=5) {
        ${'beforeDeviationPR' . $i} = 0;
    }
    for ($i = 0; $i < 100; $i+=5) {
        ${'afterDeviationSC' . $i} = 0;
    }
    for ($i = 0; $i < 100; $i+=5) {
        ${'afterDeviationPR' . $i} = 0;
    }

    $countBeforeDeviationSC = 0;
    $countAfterDeviationSC = 0;
    $countAfterDeviationPR = 0;
    $countBeforeDeviationPR = 0;

    $beforeDeviationTotalAverageSC = 0;
    $beforeDeviationTotalAveragePR = 0;
    $afterDeviationTotalAverageSC = 0;
    $afterDeviationTotalAveragePR = 0;

    include 'conn.php';

    //date range
    $range = (strtotime($endDate)- strtotime($startDate))/24/3600;

    if ($centreId == 0) {
        $clwhere = "client.CentreId is not null";
        $where = "CentreId is not null";
        $kwhere = "INNER JOIN Centre on client.CentreId=centre.CentreId where client.CentreId is not null order by $sort $order";
    } else {
        $clwhere = "client.CentreId=$centreId";
        $where = "CentreId=$centreId";
        $kwhere = "INNER JOIN Centre on client.CentreId=centre.CentreId where client.CentreId=$centreId order by $sort $order";
    }


    require_once '../Classes/PHPExcel.php';
    $objPHPExcel = new PHPExcel();
    $objPHPExcel->setActiveSheetIndex(0);
    $startDate1 = date('dmy', strtotime($startDate));
    $endDate1 = date('dmy', strtotime($endDate));
    //$objConditional1 = new PHPExcel_Style_Conditional();
    $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
    $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(18);
    $objPHPExcel->getActiveSheet()->getStyle('A3:Z3')->getFont()->setBold(true);
    $objPHPExcel->getProperties()->setTitle("EldercareReport" . $startDate . "to" . $endDate);
    if($centreId==0)
    {
        $objPHPExcel->getActiveSheet()->setCellValue('A1', 'Eldercare Report from ' . $startDate1 . ' to ' . $endDate1);
    }
    else
    {
        include 'conn.php';
        $check1 = "Select CentreName from Centre where CentreId=$centreId";
        $resultto = mysql_query($check1);
        while($row=  mysql_fetch_array($resultto))
        {
            $cname = $row['CentreName'];
        }
        $objPHPExcel->getActiveSheet()->setCellValue('A1', 'Eldercare Report from ' . $startDate1 . ' to ' . $endDate1. ' for '.$cname);
    }

    $objPHPExcel->getActiveSheet()->mergeCells('A1:N1');
    $objPHPExcel->getActiveSheet()->getStyle('A1:N1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);


    include 'conn.php';

    $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(9);
    $objPHPExcel->getActiveSheet()->mergeCells('C3:D3');
    $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
    $objPHPExcel->getActiveSheet()->SetCellValue('A3', 'S/No');
    $objPHPExcel->getActiveSheet()->SetCellValue('B3', 'NRIC');
    $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(13);
    $objPHPExcel->getActiveSheet()->SetCellValue('C3', 'Name');
    $column = 'E';
    $userType = $_SESSION["userType"];
    if ($userType == 'System Admin') {
        $objPHPExcel->getActiveSheet()->SetCellValue($column . '3', 'Centre Name');
        
    $objPHPExcel->getActiveSheet()->getColumnDimension($column)->setWidth(25);
        $column++;
    }
    $objPHPExcel->getActiveSheet()->SetCellValue($column . '3', 'Citizenship');
    $objPHPExcel->getActiveSheet()->getStyle($column . '3')->getAlignment()->setWrapText(true);
    $objPHPExcel->getActiveSheet()->getColumnDimension($column)->setWidth(13);
    $column++;
    $objPHPExcel->getActiveSheet()->SetCellValue($column . '3', 'Daycare');
    $objPHPExcel->getActiveSheet()->getStyle($column . '3')->getAlignment()->setWrapText(true);
    $objPHPExcel->getActiveSheet()->getColumnDimension($column)->setWidth(13);
    $column++;
    $objPHPExcel->getActiveSheet()->SetCellValue($column . '3', 'Before Deviation');
    $objPHPExcel->getActiveSheet()->getColumnDimension($column)->setWidth(11);
    $objPHPExcel->getActiveSheet()->getStyle($column . '3')->getAlignment()->setWrapText(true);
    $column++;
    $objPHPExcel->getActiveSheet()->SetCellValue($column . '3', 'After Deviation');
    $objPHPExcel->getActiveSheet()->getStyle($column . '3')->getAlignment()->setWrapText(true);
    $objPHPExcel->getActiveSheet()->getColumnDimension($column)->setWidth(11);
    $column++;

    $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(13);
    $objPHPExcel->getActiveSheet()->getColumnDimension('L')->setWidth(13);


    //Date columns
    $sqlDBManager = new SqlDBManager();
    
    $sql="select date from holiday";
    $res=$sqlDBManager->queryRows($sql);
    
    $Holiday=array();

    if(!empty($res)){
        for($i=0;$i<count($res);$i++)
        {
            array_push($Holiday, $res[$i]);
        }
    }

    $rowCount = 3;
    for($i=0;$i<=$range;$i++){
        $date=Date('Y-m-d', strtotime($startDate."+".$i." days"));
        for($j=0;$j<count($Holiday);$j++){
            if($Holiday[$j][0]==Date('Y-m-d', strtotime($startDate."+".$i." days")) || date('l', strtotime(Date('Y-m-d', strtotime($startDate."+".$i." days"))))== "Sunday" || date('l', strtotime(Date('Y-m-d', strtotime($startDate."+".$i." days"))))== "Saturday"){
                $i++;
            }       
        }
        $realDate=Date('d/m/Y', strtotime($startDate."+".$i." days"));
        $objPHPExcel->getActiveSheet()->SetCellValue($column . $rowCount, $realDate);
        $column++;
    }

    /*$count = 1;
    $checkDateSNo = $startDate;
    while ($checkDateSNo <= $endDate) {
        $sqlhols = "Select count(*) from holiday where Date='$checkDateSNo'";
        $resulthols = mysql_query($sqlhols);

        while ($row = mysql_fetch_array($resulthols)) {
            if ($row['count(*)'] == 0) {
                $day = date('l', strtotime($checkDateSNo));
                if ($day != "Sunday" && $day != "Saturday") {
                    $objPHPExcel->getActiveSheet()->SetCellValue($column . $rowCount, $count.' Day');
                    $checkDateSNo = date('Y-m-d', strtotime('+1 day', strtotime($checkDateSNo)));
                    $count++;
                    $column++;
                } else {
                    $checkDateSNo = date('Y-m-d', strtotime('+1 day', strtotime($checkDateSNo)));
                }
            } else {
                $checkDateSNo = date('Y-m-d', strtotime('+1 day', strtotime($checkDateSNo)));
            }
        }
    }*/

    $objPHPExcel->getActiveSheet()->SetCellValue($column . $rowCount, 'Total');
    $rowCount++;

    $ttlattendance = 0;
    include 'conn.php';
    $sql = "SELECT * from Client $kwhere";
    $result = mysql_query($sql,$conn);
    $sno = 1;
    while ($row = mysql_fetch_array($result)) {
        $objPHPExcel->getActiveSheet()->mergeCells('C' . $rowCount . ':D' . $rowCount);
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $rowCount, $sno);
        $objPHPExcel->getActiveSheet()->SetCellValue('B' . $rowCount, $row['NRIC']);
        $objPHPExcel->getActiveSheet()->SetCellValue('C' . $rowCount, $row['CustomerName']);
        $column = 'E';
        if ($userType == 'System Admin') {
            $objPHPExcel->getActiveSheet()->SetCellValue($column . $rowCount, $row['CentreName']);
            //$objPHPExcel->getActiveSheet()->getStyle($column . $rowCount)->getAlignment()->setWrapText(true);
            $column++;
        }
        $objPHPExcel->getActiveSheet()->SetCellValue($column . $rowCount, $row['Citizenship']);
        $column++;
        $objPHPExcel->getActiveSheet()->SetCellValue($column . $rowCount, $row['Daycare']);
        $column++;
        $objPHPExcel->getActiveSheet()->SetCellValue($column . $rowCount, $row['BeforeDeviation']);
        $column++;
        $objPHPExcel->getActiveSheet()->SetCellValue($column . $rowCount, $row['AfterDeviation']);
        $column++;

        $checkDate = $startDate;
        $workingday = 0;
        $attendance = 0;

        for($i=0;$i<=$range;$i++){
            $date=Date('Y-m-d', strtotime($startDate."+".$i." days"));
            for($j=0;$j<count($Holiday);$j++){
                if($Holiday[$j][0]==Date('Y-m-d', strtotime($startDate."+".$i." days")) || date('l', strtotime(Date('Y-m-d', strtotime($startDate."+".$i." days"))))== "Sunday" || date('l', strtotime(Date('Y-m-d', strtotime($startDate."+".$i." days"))))== "Saturday"){
                    $i++;
                }       
            }
            $realDate=Date('d/m/Y', strtotime($startDate."+".$i." days"));
            $sqlattendance = "Select * from attendancerecord where date(SignInTimeStamp)='$realDate' and NRIC='" . $row['NRIC'] . "' and $where";
            $resultattendance = mysql_query($sqlattendance);
            if (mysql_num_rows($resultattendance) == 1) {
                $objPHPExcel->getActiveSheet()->setCellValue($column . $rowCount, '1');
                $attendance++;
            } else {
                $objPHPExcel->getActiveSheet()->setCellValue($column . $rowCount, '0');
            }
            $workingday++;
            $column++;  
        }

        $ttlattendance += $attendance;
        $avg = $ttlattendance / $sno;
        $objPHPExcel->getActiveSheet()->setCellValue($column . $rowCount, $attendance);
        //$str.="," . $attendance . "\n";
        $styleArray = array(
            'borders' => array(
                'allborders' => array(
                    'style' => PHPExcel_Style_Border::BORDER_THIN
                )
            )
        );
        $style2Array = array(
            'borders' => array(
                'allborders' => array(
                    'style' => PHPExcel_Style_Border::BORDER_THICK, PHPExcel_Style_Border::BORDER_DOUBLE
                )
            )
        );
        $objPHPExcel->getActiveSheet()->getStyle('A3:' . $column . $rowCount)->applyFromArray($styleArray);
        $sno++;
        $rowCount++;

        //Before Deviation
        for ($i = 0; $i <= 100; $i+=5) {
            if ($row['Citizenship'] == 'SC' && $row['BeforeDeviation'] == $i) {
                ${'beforeDeviationSC' . $i} += $attendance;
            } else if ($row['Citizenship'] == 'PR' && $row['BeforeDeviation'] == $i) {
                ${'beforeDeviationPR' . $i} += $attendance;
            }
            if ($row['Citizenship'] == 'SC' && $row['AfterDeviation'] == $i) {
                ${'afterDeviationSC' . $i} += $attendance;
            } else if ($row['Citizenship'] == 'PR' && $row['AfterDeviation'] == $i) {
                ${'afterDeviationPR' . $i} += $attendance;
            }
        }

    }
    $objPHPExcel->getActiveSheet()->setCellValue('A' . $rowCount, 'Average Attendance:');
    $objPHPExcel->getActiveSheet()->mergeCells('A' . $rowCount . ':B' . $rowCount);
    $objPHPExcel->getActiveSheet()->getStyle('A' . $rowCount . ':C' . $rowCount)->getFont()->setBold(true);
    $objPHPExcel->getActiveSheet()->setCellValue('C' . $rowCount, $avg);
    $rowCount++;
    $rowCount++;
    $rowCount1 = $rowCount;
    $rowOrgCount1 = $rowCount;
    //Before Deviation first row
    $objPHPExcel->getActiveSheet()->getStyle('B' . $rowCount . ':G' . $rowCount)->getFont()->setBold(true);
    $objPHPExcel->getActiveSheet()->mergeCells('B' . $rowCount . ':G' . $rowCount);
    $objPHPExcel->getActiveSheet()->getStyle('B' . $rowCount . ':G' . $rowCount)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    //After Deviation first row
    $objPHPExcel->getActiveSheet()->getStyle('I' . $rowCount . ':N' . $rowCount)->getFont()->setBold(true);
    $objPHPExcel->getActiveSheet()->mergeCells('I' . $rowCount . ':N' . $rowCount);
    $objPHPExcel->getActiveSheet()->getStyle('I' . $rowCount . ':N' . $rowCount)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    //setting title header
    $objPHPExcel->getActiveSheet()->setCellValue('B' . $rowCount, 'Before Deviation');
    $objPHPExcel->getActiveSheet()->setCellValue('I' . $rowCount, 'After Deviation');
    $rowCount++;
    //setting 2nd rows for BD
    $objPHPExcel->getActiveSheet()->SetCellValue('B' . $rowCount, 'Subsidy Level');
    $objPHPExcel->getActiveSheet()->SetCellValue('C' . $rowCount, 'SC');
    $objPHPExcel->getActiveSheet()->SetCellValue('D' . $rowCount, 'Average');
    $objPHPExcel->getActiveSheet()->SetCellValue('E' . $rowCount, 'Subsidy Level');
    $objPHPExcel->getActiveSheet()->SetCellValue('F' . $rowCount, 'PR');
    $objPHPExcel->getActiveSheet()->SetCellValue('G' . $rowCount, 'Average');
    //setting 2nd rows for AD
    $objPHPExcel->getActiveSheet()->SetCellValue('I' . $rowCount, 'Subsidy Level');
    $objPHPExcel->getActiveSheet()->SetCellValue('J' . $rowCount, 'SC');
    $objPHPExcel->getActiveSheet()->SetCellValue('K' . $rowCount, 'Average');
    $objPHPExcel->getActiveSheet()->SetCellValue('L' . $rowCount, 'Subsidy Level');
    $objPHPExcel->getActiveSheet()->SetCellValue('M' . $rowCount, 'PR');
    $objPHPExcel->getActiveSheet()->SetCellValue('N' . $rowCount, 'Average');
    $rowCount++;
    $rowOrgCount = $rowCount;

    //BD table info
    //BEFORE DEVIATION DETAILS
    for ($i = 100; $i >= 0; $i-=5) {
        $objPHPExcel->getActiveSheet()->SetCellValue('B' . $rowCount, $i . '%');
        $sqlbdsc = "SELECT count(*) from client where BeforeDeviation=$i and Citizenship='SC' and $where";
        $sqlbdpr = "SELECT count(*) from client where BeforeDeviation=$i and Citizenship='PR' and $where";
        $resultbdsc = mysql_query($sqlbdsc);
        $resultbdpr = mysql_query($sqlbdpr);

        while ($rowbdsc = mysql_fetch_array($resultbdsc)) {
            if ($rowbdsc['count(*)'] == 0) {
                $objPHPExcel->getActiveSheet()->SetCellValue('C' . $rowCount, '0');
                $objPHPExcel->getActiveSheet()->SetCellValue('D' . $rowCount, '0');
            } else {
                $objPHPExcel->getActiveSheet()->SetCellValue('C' . $rowCount, $rowbdsc['count(*)']);

                $countBeforeDeviationSC +=$rowbdsc['count(*)'];
                for ($j = 0; $j <= 100; $j+=5) {
                    if ($j == $i) {
                        $average = round(${'beforeDeviationSC' . $j} / $workingday, 5);
                        $objPHPExcel->getActiveSheet()->SetCellValue('D' . $rowCount, $average);
                        $beforeDeviationTotalAverageSC +=$average;
                    }
                }
            }
        }

        $objPHPExcel->getActiveSheet()->SetCellValue('E' . $rowCount, $i . '%');

        while ($rowbdpr = mysql_fetch_array($resultbdpr)) {
            if ($rowbdpr['count(*)'] == 0) {
                $objPHPExcel->getActiveSheet()->SetCellValue('F' . $rowCount, '0');
                $objPHPExcel->getActiveSheet()->SetCellValue('G' . $rowCount, '0');
            } else {
                $objPHPExcel->getActiveSheet()->SetCellValue('F' . $rowCount, $rowbdpr['count(*)']);

                $countBeforeDeviationPR +=$rowbdpr['count(*)'];
                for ($j = 0; $j <= 100; $j+=5) {
                    if ($j == $i) {
                        $average = round(${'beforeDeviationPR' . $j} / $workingday, 5);
                        $objPHPExcel->getActiveSheet()->SetCellValue('G' . $rowCount, $average);
                        $beforeDeviationTotalAveragePR +=$average;
                    }
                }
            }
        }
        $rowCount++;
    }
    $objPHPExcel->getActiveSheet()->getStyle('B' . $rowCount . ':G' . $rowCount)->getFont()->setBold(true);
    $objPHPExcel->getActiveSheet()->SetCellValue('B' . $rowCount, 'Total');
    $objPHPExcel->getActiveSheet()->SetCellValue('C' . $rowCount, $countBeforeDeviationSC);
    $objPHPExcel->getActiveSheet()->SetCellValue('D' . $rowCount, $beforeDeviationTotalAverageSC);
    $objPHPExcel->getActiveSheet()->SetCellValue('E' . $rowCount, 'Total');
    $objPHPExcel->getActiveSheet()->SetCellValue('F' . $rowCount, $countBeforeDeviationPR);
    $objPHPExcel->getActiveSheet()->SetCellValue('G' . $rowCount, $beforeDeviationTotalAveragePR);

    //AFTER DEVIATION TABLE INFO
    for ($j = 100; $j >= 0; $j-=5) {
        $objPHPExcel->getActiveSheet()->SetCellValue('I' . $rowOrgCount, $j . '%');
        $sqladsc = "SELECT count(*) from client where AfterDeviation=$j and Citizenship='SC' and $where";
        $sqladpr = "SELECT count(*) from client where AfterDeviation=$j and Citizenship='PR' and $where";
        $resultadsc = mysql_query($sqladsc);
        $resultadpr = mysql_query($sqladpr);

        while ($rowadsc = mysql_fetch_array($resultadsc)) {
            if ($rowadsc['count(*)'] == 0) {
                $objPHPExcel->getActiveSheet()->SetCellValue('J' . $rowOrgCount, '0');
                $objPHPExcel->getActiveSheet()->SetCellValue('K' . $rowOrgCount, '0');
            } else {

                $objPHPExcel->getActiveSheet()->SetCellValue('J' . $rowOrgCount, $rowadsc['count(*)']);

                $countAfterDeviationSC +=$rowadsc['count(*)'];
                for ($k = 0; $k <= 100; $k+=5) {
                    if ($k == $j) {
                        $average = round(${'afterDeviationSC' . $k} / $workingday,5);
                        $objPHPExcel->getActiveSheet()->SetCellValue('K' . $rowOrgCount, $average);
                        $afterDeviationTotalAverageSC +=$average;
                    }
                }
            }
        }

        $objPHPExcel->getActiveSheet()->SetCellValue('L' . $rowOrgCount, $j . '%');

        while ($rowadpr = mysql_fetch_array($resultadpr)) {
            if ($rowadpr['count(*)'] == 0) {
                $objPHPExcel->getActiveSheet()->SetCellValue('M' . $rowOrgCount, '0');
                $objPHPExcel->getActiveSheet()->SetCellValue('N' . $rowOrgCount, '0');
            } else {
                $objPHPExcel->getActiveSheet()->SetCellValue('M' . $rowOrgCount, $rowadpr['count(*)']);
                $countAfterDeviationPR +=$rowadpr['count(*)'];
                for ($k = 0; $k <= 100; $k+=5) {
                    if ($k == $j) {
                        $average = ${'afterDeviationPR' . $k} / $workingday;
                        $objPHPExcel->getActiveSheet()->SetCellValue('N' . $rowOrgCount, $average);
                        $afterDeviationTotalAveragePR +=$average;
                    }
                }

            }
        }
        $rowOrgCount++;
    }
    $objPHPExcel->getActiveSheet()->getStyle('I' . $rowCount . ':N' . $rowCount)->getFont()->setBold(true);


    $objPHPExcel->getActiveSheet()->SetCellValue('I' . $rowCount, 'Total');
    $objPHPExcel->getActiveSheet()->SetCellValue('J' . $rowCount, $countAfterDeviationSC);
    $objPHPExcel->getActiveSheet()->SetCellValue('K' . $rowCount, $afterDeviationTotalAverageSC);
    $objPHPExcel->getActiveSheet()->SetCellValue('L' . $rowCount, 'Total');
    $objPHPExcel->getActiveSheet()->SetCellValue('M' . $rowCount, $countAfterDeviationPR);
    $objPHPExcel->getActiveSheet()->SetCellValue('N' . $rowCount, $afterDeviationTotalAveragePR);

    $rowCount = $rowCount - 1;
    $rowOrgCount = $rowOrgCount - 1;
    $objPHPExcel->getActiveSheet()->getStyle('B' . $rowCount1 . ':' . 'G' . $rowCount)->applyFromArray($styleArray);
    $objPHPExcel->getActiveSheet()->getStyle('I' . $rowOrgCount1 . ':' . 'N' . $rowOrgCount)->applyFromArray($styleArray);
    $rowCount++;
    $rowOrgCount++;
    $objPHPExcel->getActiveSheet()->getStyle('B' . $rowCount . ':' . 'G' . $rowCount)->applyFromArray($style2Array);
    $objPHPExcel->getActiveSheet()->getStyle('I' . $rowOrgCount . ':' . 'N' . $rowOrgCount)->applyFromArray($style2Array);
    //creating the excel file
    //MAILING
    //header('Content-Disposition: attachment;filename="EldercareReport' . $startDate1 . 'to' . $endDate1 . '.xlsx"');
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    header('Content-Type: application/vnd.ms-excel');
    if($centreId==0)
    {
        header('Content-Disposition: attachment;filename="EldercareReport' . $startDate1 . 'to' . $endDate1 . '.xlsx"');
    }
    else
    {
        include 'conn.php';
        $check1 = "Select CentreName from Centre where CentreId=$centreId";
        $resultto = mysql_query($check1);
        while($row=  mysql_fetch_array($resultto))
        {
            $cname = $row['CentreName'];
        }
        header('Content-Disposition: attachment;filename="EldercareReport' . $startDate1 . 'to' . $endDate1 . '-'.$cname.'.xlsx"');
    }


    header('Cache-Control: max-age=0');
    $objWriter->save('php://output');
    exit();
    //$objWriter->save(str_replace('export_1.php', 'EldercareReport' . $startDate1 . 'to' . $endDate1 . '.xlsx', __FILE__));
    //$objWriter->save('php://output');
?>

